package com.ggk911.pharmacy_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ggk911.pharmacy_system.entity.PutInStorage;
import com.ggk911.pharmacy_system.service.impl.PutInStorageServiceImpl;
import com.ggk911.pharmacy_system.util.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 入库单更控制层
 *
 * @author ggk-911
 * @version 1.0 2023/4/11 16:35
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@CrossOrigin
@RequestMapping("/putInStorage")
public class PutInStorageController {
    private final PutInStorageServiceImpl service;

    //GET查询
    @GetMapping("/queryAll")
    public RestResult queryAll(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size,
                               Integer purchaseId,
                               Integer userId) {
        Page<PutInStorage> putInStoragePage = new Page<>(page, size);
        QueryWrapper<PutInStorage> wrapper = new QueryWrapper<>();

        wrapper.like(purchaseId != null, "purchase_id", purchaseId);
        wrapper.like(userId != null, "user_id", userId);

        service.pageAd(putInStoragePage, wrapper);
        return RestResult.ok("查询成功", putInStoragePage);
    }

    //POST添加
    @PostMapping("/insert")
    public RestResult insert(PutInStorage putInStorage) {
        if (service.save(putInStorage)) {
            return RestResult.ok("添加成功, 返回编号为：" + putInStorage.getPutInStorageId(), putInStorage.getPutInStorageId());
        }
        return RestResult.error("添加失败");
    }

    //PUT修改
    @PutMapping("/update")
    public RestResult update(PutInStorage putInStorage,
                             Integer putInStorageId) {
        QueryWrapper<PutInStorage> wrapper = new QueryWrapper();
        wrapper.eq(putInStorageId != null, "put_in_storage_id", putInStorageId);

        if (service.update(putInStorage, wrapper)) {
            return RestResult.ok("修改成功");
        }
        return RestResult.error("修改失败");
    }

    //DELETE删除
    @DeleteMapping("/delete")
    public RestResult delete(Integer putInStorageId) {
        QueryWrapper<PutInStorage> wrapper = new QueryWrapper();
        wrapper.eq(putInStorageId != null, "put_in_storage_id", putInStorageId);

        if (service.remove(wrapper)) {
            return RestResult.ok("删除成功");
        }
        return RestResult.error("删除失败");
    }
}
